﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_ProcessDoing")]
    public class P_ProcessDoing : IEntity<P_ProcessDoing>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int ProcessID { get; set; }
        public int Type { get; set; }
        public int ProjectID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(50)]
        public string ProcessName { get; set; }
        public int SignType { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(1024)]
        public string SignUsers { get; set; }
        public int ChangeState { get; set; }
        public int DoingState { get; set; }
        public DateTime ChangeTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string DoneUser { get; set; }

        public override P_ProcessDoing Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_ProcessDoing Find(System.Linq.Expressions.Expression<Func<P_ProcessDoing, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProcessDoing.FirstOrDefault(where);
                if (item != null)
                {
                    this.ChangeState = item.ChangeState;
                    this.ChangeTime = item.ChangeTime;
                    this.DoingState = item.DoingState;
                    this.DoneUser = item.DoneUser;
                    this.ID = item.ID;
                    this.ProcessName = item.ProcessName;
                    this.SignType = item.SignType;
                    this.SignUsers = item.SignUsers;
                    this.ProjectID = item.ProjectID;
                    this.Type = item.Type;
                    this.ProcessID = item.ProcessID;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_ProcessDoing, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProcessDoing.Any(where);
            }
        }
    }
}